package com.levy.contronller.javaWeb.findImageTest;

/**
 * @author levy
 * @description
 * @date 2021/8/19 17:53
 */

import static org.junit.Assert.*;

import java.io.*;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.HexUtil;
import org.junit.Test;

/**
 * ZLib压缩测试用例
 *
 * @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</a>
 * @version 1.0
 * @since 1.0
 */
public class ZLibUtilsTest {

    /**
     * the traditional io way
     *
     * @param filename
     * @return
     * @throws IOException
     */
    public static byte[] toByteArray(String filename) throws IOException {

        File f = new File(filename);
        if (!f.exists()) {
            throw new FileNotFoundException(filename);
        }

        ByteArrayOutputStream bos = new ByteArrayOutputStream((int) f.length());
        BufferedInputStream in = null;
        try {
            in = new BufferedInputStream(new FileInputStream(f));
            int buf_size = 1024;
            byte[] buffer = new byte[buf_size];
            int len = 0;
            while (-1 != (len = in.read(buffer, 0, buf_size))) {
                bos.write(buffer, 0, len);
            }
            return bos.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
            throw e;
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            bos.close();
        }
    }


    @Test
    public final void testBytes() throws IOException {
//        System.err.println("字节压缩／解压缩测试");
//        String inputStr = "snowolf@zlex.org;dongliang@zlex.org;zlex.dongliang@zlex.org";
//        System.err.println("输入字符串:\t" + inputStr);
//        byte[] input = inputStr.getBytes();
//        System.err.println("输入字节长度:\t" + input.length);
//
//        byte[] data = ZLibUtils.compress(input);
//        System.err.println("压缩后字节长度:\t" + data.length);

        String dd ="E:\\BaiduNetdiskDownload\\老照片\\txt\\民国以后\\38年花园口决堤后惨状";
        byte[] bytes = toByteArray(dd);
        byte[] output = ZLibUtils.decompress(bytes);
        System.err.println("解压缩后字节长度:\t" + output.length);


        char[] chars = HexUtil.encodeHex(output);
        System.out.println(chars);


//        String outputStr = new String(output);
//        System.err.println("输出字符串:\t" + outputStr);

//        assertEquals(inputStr, outputStr);
    }

    @Test
    public final void testFile() {
        String filename = "zlib";
        File file = new File(filename);
        System.err.println("文件压缩／解压缩测试");
        String inputStr = "snowolf@zlex.org;dongliang@zlex.org;zlex.dongliang@zlex.org";
        System.err.println("输入字符串:\t" + inputStr);
        byte[] input = inputStr.getBytes();
        System.err.println("输入字节长度:\t" + input.length);

        try {

            FileOutputStream fos = new FileOutputStream(file);
            ZLibUtils.compress(input, fos);
            fos.close();
            System.err.println("压缩后字节长度:\t" + file.length());
        } catch (Exception e) {
            fail(e.getMessage());
        }

        byte[] output = null;

        try {
            FileInputStream fis = new FileInputStream(file);
            output = ZLibUtils.decompress(fis);
            fis.close();

        } catch (Exception e) {
            fail(e.getMessage());
        }
        System.err.println("解压缩后字节长度:\t" + output.length);
        String outputStr = new String(output);
        System.err.println("输出字符串:\t" + outputStr);

        assertEquals(inputStr, outputStr);
    }
}
